package org.k.GUI.explorer;

import javax.imageio.ImageIO;
import javax.swing.*;


import com.mxcyk.serviceLogic.*;
import com.uujava.minibox.ui.button.ImageJButton;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
 * 功能：桌面显示即把桌面上的全部显示到本窗体内
 * 使用方法：
 * 				水平间距，垂直间距
 * WindowsHome(int hgap, int vgap)
 * @author k
 *日期：2012-8-7
 */
public class WindowsHome extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1427183537871141018L;
	GetAction getaction = null;// 监听类
	JButton jb1, jb2, jb3, jb4;
	JPanel jpCenter, jpSouth, jp2, jp3, jp4, jp5;
	Appfile[] appls;
	CardLayout cl;
	FlowLayout flowla;
	// 网格的行列数为
	final int ROWS = 9;
	final int COLS = 9;

	public static void main(String[] args) {
		JFrame jf = new JFrame();
		String Path = System.getProperty("user.dir");// 获得当前路径
		Path = Path + "\\常用应用";
		WindowsHome jpwin = new WindowsHome(Path,5, 5);

		jf.setSize(800, 600);
		jf.add(jpwin);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设定窗体关闭后自动退出进程
		jf.setVisible(true);
	}

	//=================================================================================================
	private Image backImage=null;//背景图片
	
	//传入图片路径设置背景
	public void setBackImage(File file){
		try {
			backImage = ImageIO.read(file);
		} catch (IOException ex) {
			backImage=null;//如果获得图片路径为空则image=null
		}
	}
	
	//传入图片路径设置背景
	public void setBackImage(String file){
		try {
			File f=new File(file);
			backImage = ImageIO.read(f);
		} catch (IOException ex) {
			backImage=null;//如果获得图片路径为空则image=null
		}
	}
	
	//传入图片设置背景
	public void setBackImage(Image image){
		this.backImage=image;
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Image im=new ImageIcon("").getImage();
		if ((backImage != null) && (!backImage.equals(im)))// 图标不为空且图片不为空
			g.drawImage(backImage, 0, 0, this.getWidth(), this.getHeight(), null);
		
	}
//======================================================================================================
	
	/**
	 * @param path
	 * 				//设置显示目录内文件的路径
	 * @param hgap
	 *            //按钮间的水平间隙
	 * @param vgap
	 *            //按钮间的垂直间隙
	 */
	public WindowsHome(String path,int hgap, int vgap) {
		this.setLayout(new BorderLayout());
		flowla = new FlowLayout(FlowLayout.LEFT, hgap, vgap);
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel();
		jp5 = new JPanel();
		jp2.setLayout(flowla);
		jp3.setLayout(flowla);
		jp4.setLayout(flowla);
		jp5.setLayout(flowla);

		jb1 = new JButton("天");
		jb2 = new JButton("地");
		jb3 = new JButton("玄");
		jb4 = new JButton("黄");
		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jb3.addActionListener(this);
		jb4.addActionListener(this);

		cl = new CardLayout();
		jpCenter = new JPanel(cl);
		// ==================================================================
		
		FileList getapp = new FileList(path);
		MakeFileLise mfl = getapp.getApp();
		ArrayList<Appfile> appfile = mfl.getLs();
		getaction = new GetAction(appfile);// 为监听类传入操作的应用列表
		System.out.println("共有应用：" + appfile.size());
		JPanel jps[] = { jp2, jp3, jp4, jp5 };
		addAppsShow(0, jps, appfile, hgap, vgap);
		// ==================================================================

		// 把3个Panel放入jp1（CardLayout）
		// 在默认情况下显示的是第一张卡片
		jpCenter.add(jp2, "0");
		jpCenter.add(jp3, "1");
		jpCenter.add(jp4, "2");
		jpCenter.add(jp5, "3");

		jpSouth = new JPanel();
		jpSouth.add(jb1);
		jpSouth.add(jb2);
		jpSouth.add(jb3);
		jpSouth.add(jb4);
		this.add(jpCenter, "Center");
		this.add(jpSouth, "South");

	}

	int CardNumb = 0;// 正在使用第几张卡片
	int cont = ((ROWS * COLS) - 1);// 每张卡片上放置的[]jp的最大下标

	/**
	 * 功能：为网格中添加按钮控件
	 * 
	 * @param index应用列表的起始索引
	 * @param jp添加到的容器数组
	 * @param appfile应用列表
	 * @param hgap
	 *            //按钮间的水平间隙
	 * @param vgap
	 *            //按钮间的垂直间隙
	 */
	private void addAppsShow(int index, JPanel[] jp, ArrayList<Appfile> appfile,
			int hgap, int vgap) {

		if (CardNumb >= 4) {
			System.out.println("应用数量超标！");
			return;
		}
		JPanel jp2 = jp[CardNumb++];

		// 获得初始化按钮上图片的大小
		int width = 32;
		int height = 32;
		System.out.println("初始化按钮的大小" + width + "  " + height);
		int flag1 = index;
		int size=appfile.size();
		for (int i = 0; i < size; i++) {
			if (flag1 < size) {
				ImageJButton jb = new ImageJButton(appfile.get(flag1).getAppName(),
						appfile.get(flag1).getAppIcon(), width, height);
				jb.setToolTipText(appfile.get(flag1).getAppName());// 当鼠标移动到按钮上显示的提醒
				jb.setName("" + " " + flag1);// 设置控件的名字为为监听者准备
				jb.addMouseListener(getaction);
				jp2.add(jb);
				if (flag1 < cont) {
					flag1++;
				} else {
					flag1++;
					break;
				}
			}
		}

		cont += ROWS * COLS;
		if (flag1 < size) {
			addAppsShow(flag1, jp, appfile, hgap, vgap);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == jb1) {
			cl.show(jpCenter, "0");
		} else if (e.getSource() == jb2) {
			cl.show(jpCenter, "1");
		} else if (e.getSource() == jb3) {
			cl.show(jpCenter, "2");
		} else if (e.getSource() == jb4) {
			cl.show(jpCenter, "3");
		}

	}

}
